package com.junko.service.back.impl;

import com.junko.core.base.BaseServiceImpl;
import com.junko.dao.back.RoleDao;
import com.junko.model.back.Role;
import com.junko.service.back.IGroupService;
import com.junko.service.back.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleDao roleDao;
    @Resource
    private IGroupService groupService;

    @Override
    public List<Role> findByRoleIds(String roleIds) {
        return roleDao.findByRoleIds(StringUtils.split(roleIds, ","));
    }

    /**
     * 组角色选择
     *
     * @param id 分组ID
     */
    @Override
    public Map<String, Object> select(String id) {
        List<Role> noCheckedList;
        List<Role> checkedList = new ArrayList<Role>();
        String roleId = groupService.selectByPrimaryKey(id).getRoleIds();
        if (null != roleId && !roleId.equals("")) {
            noCheckedList = roleDao.findNoChecked(roleId.split(","));
            checkedList = roleDao.findChecked(roleId.split(","));
        } else {
            noCheckedList = roleDao.findAllNoChecked();
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("noCheckedList", noCheckedList);
        map.put("checkedList", checkedList);
        return map;
    }
}